<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :permission="permissionList"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @row-del="rowDel"
      @on-load="onLoad"
    >
      <!-- <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" icon="el-icon-delete" plain style="border: 0; background-color: transparent !important" @click="handleDelete(row)">删 除 </el-button>
      </template> -->
      <span slot="footer" class="footer">
        <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" :disabled="ids.length > 0 ? false : true" @click="repairSuccessVisible = true"
          >保 存</el-button
        >
        <el-button size="small" icon="el-icon-circle-close" @click="selectionClear">清 空</el-button>
      </span>
    </avue-crud>
    <el-dialog title="填写报废原因" :visible.sync="repairSuccessVisible" :modal-append-to-body="false" :show-close="true" :close-on-press-escape="false" width="40%">
      <el-form ref="repairSuccessForm" :model="repairSuccessForm" :rules="rulesSuccessForm">
        <el-form-item label="报废原因" prop="scrapReason">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="repairSuccessForm.scrapReason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="scrapIn">保 存</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import { submit, add } from '@/api/repair/failurerecord';
import { getList } from '@/api/repair/repairSite';
import { flagScrap } from '@/api/repair/failureconfirm';

import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: false,
      selectionList: [],
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      option: {
        height: '500',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,
        selection: true,
        addBtn: false,
        editBtn: false,
        delBtn: true,
        emptyBtn: false,
        searchBtnText: '录入',
        menuWidth: 200, //弹框内字段宽度属性
        searchLabelWidth: '120',
        column: [
          {
            label: '包装条码',
            prop: 'name',
            search: true,
            rules: [
              {
                required: true,
                message: '请输入内容',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '数量',
            prop: 'code'
          },
          {
            label: '仓库',
            prop: 'failureName'
          },
          {
            label: '储位',
            prop: 'workOrderNo'
          },
          {
            label: '物料编码',
            prop: 'workshopName'
          },
          {
            label: '物料名称',
            prop: 'lineName'
          },
          {
            label: '物料批次',
            prop: 'partNo'
          }
        ]
      },
      data: [],
      repairSuccessVisible: false,
      repairSuccessForm: {
        scrapReason: ''
      },
      rulesSuccessForm: {
        scrapReason: [{ required: true, message: '请输入报废原因', trigger: 'blur' }]
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.sequencedefinition_add, false),
        viewBtn: this.vaildData(this.permission.sequencedefinition_view, false),
        delBtn: this.vaildData(this.permission.sequencedefinition_delete, false),
        editBtn: this.vaildData(this.permission.sequencedefinition_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    }
  },
  methods: {
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.data = this._remove(this.data, row);
        })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    searchReset() {
      this.query = {};
    },
    searchChange(params, done) {
      add(params).then((res) => {
        const data = res.data.data;
        if (this.data.length !== 0) {
          this._forEach(this.data, (item) => {
            if (item.productSequenceNumber !== data.productSequenceNumber) {
              this.data.push(data);
            }
          });
        } else {
          this.data.push(data);
        }
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      });
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    refreshChange() {},
    submitEvent() {
      submit(this.data).then((res) => {
        this.data = [];
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
      });
    },
    // 手动报废
    scrapIn() {
      this.$refs.repairSuccessForm.validate((valid, done) => {
        flagScrap(this.ids.join(','), [], this.repairSuccessForm.scrapReason, 1).then((res) => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          // done();
          this.$refs.repairSuccessForm.resetFields();
          this.onLoad();
          this.repairSuccessVisible = false;
        });
      });
    },
    onSubmit() {},
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        // [] || dataListTable
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style scoped>
.footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>
